package k2_oop.p6_relation.demo05;

public class Dictionary {

    //创建一个长度为10的类型为Word的一维数组
    public Word[] words = new Word[10];

    int wordCount = 0;

    /**
     * 1.此类只在词典类内部使用，所以定义为内部类
     * 2.此类对象表示某一个具体的单词，需要属性 english和 chinese
     */
    public class Word{

        public String english;
        public String chinese;

        public Word(String english, String chinese) {
            this.english = english;
            this.chinese = chinese;
        }
    }

    public String addWord(String english,String chinese){

        if(wordCount<words.length){
            for (int i = 0; i < wordCount; i++) {
                //如果内部类的english这个属性与参数一致，则用参数的中文覆盖掉内部类中的chinese属性
                if(words[i].english.equals(english)){
                    words[i].chinese = chinese;
                    return "您要添加的单词已经存在，已经为您更新";
                }
            }
            words[wordCount] = new Word(english,chinese);
            wordCount++;
            return "添加成功";
        }else{
            return "词典已经满了，无法录入！";
        }
    }

    public String transform(String english){
        for (Word word : words) {
            if (word.english.equals(english)){
                return word.chinese;
            }
        }
        return "查无此单词";
    }

    public void display(){
        if(wordCount==0){
            System.out.println("词典为空");
        }

        for (int i = 0; i < wordCount ; i++) {
            System.out.println(words[i].english+":"+words[i].chinese);
        }

        System.out.println("======================================"+this.wordCount);
    }

}
